package com.itfenghuang.day01.day02;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.*;

//请求对象
public class A03_Request extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 解决设置post请求中文乱码问题
        // public void setCharacterEncoding(String env)
        //get请求 Tomcat8.5之后没有乱码问题
        req.setCharacterEncoding("utf-8");

        //请求对象的常用方法之获取路径的方法
        //获取虚拟目录的名称
        String contextPath = req.getContextPath();
        System.out.println("getContextPath()"+contextPath);
        //获取Servlet映射路径
        String servletPath = req.getServletPath();
        System.out.println("getServletPath()"+servletPath);
        //获取访问者IP地址
        String remoteAddr = req.getRemoteAddr();
        System.out.println("getRemoteAddr()"+remoteAddr);
        //获取到请求的消息数据
        String queryString = req.getQueryString();
        System.out.println("getQueryString()"+queryString);
        //获取统一资源标识符
        //URL 统一资源标识符 协议+主机名+端口号+虚拟目录+访问资源路径
        StringBuffer requestURL = req.getRequestURL();
        System.out.println("getRequestURL()"+requestURL);
        //获取统一资源定位符
        //URI 统一资源定位符  虚拟目录+访问资源路径
        String requestURI = req.getRequestURI();
        System.out.println("getRequestURI()"+requestURI);



        /**
         *  请求对象常用方法之获取请求头信息的方法
         */
        //根据请求头名称获取请求头的值 pulbic String getHeader(String name)
        String connection = req.getHeader("Connection");
        System.out.println(connection);
        String header = req.getHeader("Accept-Encoding");
        System.out.println(header);
//        req.getHeaders()

        /**
         * 请求对象常用方法之获取请求参数信息的方法
         *
         * 1). 根据请求参数名获取请求参数值
         *  public String getParameter(String name);
         * 2). 根据请求参数名获取所有请求参数值
         *  public String[] getParameterValues(String name);
         * 3). 获取所有的请求参数的名称
         *  public Enumeration<String> getParameterNames();
         * 4). 获取所有的请求参数键值对
         *  public Map<String, String[]> getParameterMap();
         */

        // 1).根据名称获取请求参数值 public String getParameter(String name);
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        System.out.println(username+password);

        // 2).根据名称获取所有数据 public String[] getParameterValues(String name);
        String[] hobbies = req.getParameterValues("hobby");
        System.out.println(Arrays.toString(hobbies));

        // 3).获取所有请求参数名称
        Enumeration<String> names = req.getParameterNames();
        while (names.hasMoreElements()){
            String s = names.nextElement();
            String para = req.getParameter(s);
            System.out.println(s+""+para);
        }

        // 4).获取所有的参数的一个键值对集合
        Map<String, String[]> parameterMap = req.getParameterMap();
        //两种遍历方法 keySet() rntrySet
        Set<Map.Entry<String, String[]>> set = parameterMap.entrySet();
        Iterator<Map.Entry<String, String[]>> it = set.iterator();
        while (it.hasNext()){
            Map.Entry<String, String[]> entry = it.next();
            //请求参数名
            String key = entry.getKey();
            //请求参数值
            String[] value = entry.getValue();
            System.out.println(key+"="+Arrays.toString(value));
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
